<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="60%"
      append-to-body
      custom-class="import-dialog"
    >
      <div class="app-container">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="房间" prop="estateName">
            <el-input
              v-model="queryParams.estateName"
              placeholder="请输入名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="承租人" prop="tenantName">
            <el-input
              v-model="queryParams.tenantName"
              placeholder="请输入名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="合同类型" prop="contractType">
            <el-select
              v-model="queryParams.contractType"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="dict in dict.type.dict_contract_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="周期类型" prop="paymentType">
            <el-select
              v-model="queryParams.paymentType"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="dict in dict.type.dict_payment_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-table
          v-loading="loading"
          :data="list"
          @selection-change="handleSelectionChange"
          ref="table"
        >
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="合同编号" prop="contractId" width="120" />
          <el-table-column
            label="房屋"
            prop="roleName"
            :show-overflow-tooltip="true"
            width="150"
          >
            <template slot-scope="scope">
              <span>{{ generateRoom(scope.row) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="租客信息"
            prop="roleKey"
            :show-overflow-tooltip="true"
            width="150"
          >
            <template slot-scope="scope">
              <span
                >{{ scope.row.tenantName }}-{{ scope.row.tenantMobile }}</span
              >
            </template>
          </el-table-column>
          <el-table-column label="合同类型" prop="contractType" width="100">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.dict_contract_type"
                :value="scope.row.contractType"
              />
            </template>
          </el-table-column>
          <el-table-column label="周期类型" prop="paymentType" width="100">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.dict_payment_type"
                :value="scope.row.paymentType"
              />
            </template>
          </el-table-column>
          <el-table-column label="租金（元）" prop="monthPrice" width="100" />
          <el-table-column label="押金（元）" prop="cashPledge" width="100" />
          <el-table-column label="押付方式" prop="payTypeId" width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.payTypeId | payType(payTypeList) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="合同周期" prop="leaseStartTime" width="100">
            <template slot-scope="scope">
              <span
                >{{ scope.row.leaseStartTime }} ~
                {{ scope.row.leaseEndTime }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            label="时长（天）"
            prop="leaseDaysNumber"
            width="100"
          />
          <el-table-column label="状态" prop="contractState" width="100">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.dict_contract_state"
                :value="scope.row.contractState"
              />
            </template>
          </el-table-column>

          <el-table-column
            label="创建时间"
            align="center"
            prop="updateTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ formatDate(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="select(scope.row)"
                >选择</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import { getHousingAllocation, listPayType } from "@/api/house/unmount";
import { listContract, getCountContractState } from "@/api/contract";
export default {
  props: {
    title: {
      default: "选择合同",
    },
    value: {
      default: false,
    },
  },
  dicts: ["dict_payment_type", "dict_contract_state", "dict_contract_type"],
  data() {
    return {
      visible: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateName: undefined,
        tenantName: undefined,
        paymentType: undefined,
        apartmentType: undefined,
        contractState: null,
        auditState: undefined,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      payTypeList: [],
    };
  },
  filters: {
    payType(value, list) {
      // console.log(list);
      let result = list.find((item) => {
        return item.id == value;
      });
      return result ? result.name : value;
    },
  },
  computed: {
    generateRoom() {
      return (row) => {
        let name = "";
        if (row.estateName) name += row.estateName;
        if (row.buildingName) name += row.buildingName + "栋";
        if (row.unitName) name += row.unitName + "单元";
        if (row.roomNumber) name += row.roomNumber + "号";
        return name;
      };
    },
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.getList();
      this.getPayType();
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listContract(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    handleSelectionChange() {},
    //获取押付方式
    getPayType() {
      listPayType().then((response) => {
        this.payTypeList = response.data;
      });
    },
    select(item) {
      this.$emit("success", item);
      this.close();
    },
    close() {
      this.resetQuery();
      this.visible = false;
    },
  },
};
</script>

  <style lang="scss" scoped>
</style>
